package it.blueocean.acanto.taglibrary.jsp.taglib.tags.commands;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HtmlPrintHelper;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

public class LinkTag extends SetComponentsPropertiesTag
{
	private static final long serialVersionUID = -8915594521321319398L;

	private final static Logger logger_ = LoggerFactory.getLogger(LinkTag.class);

	private String htmlClassExpr_;

	private String htmlStyleExpr_;

	private String htmlClass_;

	private String htmlStyle_;

	private String onClick_;

	private String onClickExpr_;

	private boolean usePlaceholder_ = true;

	private String usePlaceholderExpr_;

	public static final String PLACEHOLDER = "?";

	public int doStartTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		return 2;
	}

	public int doEndTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			printStartTag();
			localJspWriter.print(this.bodyContent.getString());
			printEndTag();
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		return 6;
	}

	protected void printStartTag()
	{
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			String str = getUrl();
			HtmlPrintHelper.printOpenedHtmlStartTag("a", this.htmlClass_, this.htmlStyle_, localJspWriter);
			HtmlPrintHelper.printAttribute("id", getId(), localJspWriter);
			HtmlPrintHelper.printAttribute("href", str, localJspWriter);
			if((isAjaxEnabled()) && (this.usePlaceholder_))
				if(this.onClick_ != null)
				{
					StringBuffer localStringBuffer = new StringBuffer();
					int i = this.onClick_.lastIndexOf("?");
					if(i != -1)
						localStringBuffer.append(this.onClick_.substring(0, i)).append("'").append(str).append("'")
								.append(this.onClick_.substring(i + 1));
					HtmlPrintHelper.printAttribute("onclick", localStringBuffer.toString(), localJspWriter);
				}
				else
					HtmlPrintHelper.printAttribute("onclick", this.onClick_, localJspWriter);
			HtmlPrintHelper.printClosedHtmlStartTag(localJspWriter);
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
	}

	protected void printEndTag()
	{
		JspWriter localJspWriter = this.pageContext.getOut();
		try
		{
			HtmlPrintHelper.printHtmlEndTag("a", localJspWriter);
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
	}

	protected void evaluateExpressions() throws JspException
	{
		super.evaluateExpressions();
		if(this.htmlClassExpr_ != null)
			this.htmlClass_ = ((String) ExpressionEvaluatorManager.evaluate("class", this.htmlClassExpr_, String.class,
					this, this.pageContext));
		else
			this.htmlClass_ = null;
		if(this.htmlStyleExpr_ != null)
			this.htmlStyle_ = ((String) ExpressionEvaluatorManager.evaluate("style", this.htmlStyleExpr_, String.class,
					this, this.pageContext));
		else
			this.htmlStyle_ = null;
		if(this.onClickExpr_ != null)
			this.onClick_ = ((String) ExpressionEvaluatorManager.evaluate("onClick", this.onClickExpr_, String.class,
					this, this.pageContext));
		else
			this.onClick_ = null;
		if(this.usePlaceholderExpr_ == null)
			return;
		Object localObject = ExpressionEvaluatorManager.evaluate("usePlaceholder", this.usePlaceholderExpr_,
				Object.class, this, this.pageContext);
		if(localObject instanceof Boolean)
			this.usePlaceholder_ = ((Boolean) localObject).booleanValue();
		else
			this.usePlaceholder_ = Boolean.valueOf(localObject.toString()).booleanValue();
	}

	public String getUsePlaceholder()
	{
		return this.usePlaceholderExpr_;
	}

	public void setUsePlaceholder(String paramString)
	{
		this.usePlaceholderExpr_ = paramString;
	}

	public void setHtmlClass(String paramString)
	{
		this.htmlClassExpr_ = paramString;
	}

	public void setStyle(String paramString)
	{
		this.htmlStyleExpr_ = paramString;
	}

	public String getHtmlClass()
	{
		return this.htmlClass_;
	}

	public String getHtmlStyle()
	{
		return this.htmlStyle_;
	}

	public String getOnClick()
	{
		return this.onClick_;
	}

	public void setOnClick(String paramString)
	{
		this.onClickExpr_ = paramString;
	}
}
